package com.example.cymjoe.huaweishopping.fragment.fragment2;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.example.cymjoe.huaweishopping.MyApp;
import com.example.cymjoe.huaweishopping.R;
import com.example.cymjoe.huaweishopping.adapter.GDTPAdapter;
import com.example.cymjoe.huaweishopping.bean.DTBean;
import com.example.cymjoe.huaweishopping.bean.GDTPBean;
import com.google.gson.Gson;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

/**
 * A simple {@link Fragment} subclass.
 */
public class JBFragment extends Fragment {
    private String prdId;
    private MyApp myApp;
    private List<String> data = new ArrayList<>();
    private GDTPAdapter adapter;
    private ViewPager viewP;
    private List<GDTPBean.SkuImgListBean> skuImgList = new ArrayList<>();
    private Timer timer1;
    private TextView skuName;
    private List<DTBean.SkuListBean> data2 = new ArrayList<>();
    private TextView num;
    private TextView numAll;

    public JBFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_jb, container, false);
        viewP = ((ViewPager) view.findViewById(R.id.viewP));

        skuName = ((TextView) view.findViewById(R.id.skuName));
        num = ((TextView) view.findViewById(R.id.num));
        numAll = ((TextView) view.findViewById(R.id.numAll));
        x.view().inject(getActivity());
        myApp = (MyApp) getActivity().getApplication();
        prdId = myApp.getSs();
        String path = "http://mw.vmall.com/product/getPrdPhotos.json?prdId=" + prdId;
        String path2 = "http://mw.vmall.com/product/getPrdInfoNew.json?prdId=" + prdId;
        getImgData(path);

        getData(path2);
        viewP.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                String ss = Integer.toString(position + 1);
                num.setText(ss);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

        timer1 = new Timer();
        try {
            timer();
        } catch (Exception e) {
            e.printStackTrace();
        }

        Log.e("path", path);
        return view;
    }

    private void timer() {
        try {

            timer1.schedule(new TimerTask() {

                @Override
                public void run() {
                    // TODO Auto-generated method stub
                    getActivity().runOnUiThread(new Runnable() {
                        // 主线程
                        @Override
                        public void run() {
                            // TODO Auto-generated method stub
                            int currentItem = viewP.getCurrentItem(); // 得到ViewPager当前显示第几页

                            int count = adapter.getCount(); // 总共有多少条

                            // 0123 4
                            if (currentItem == (count - 1)) {
                                // 如果使用第二个参数，单条显示Fragment数据可能无法加载！

                                viewP.setCurrentItem(0);
                                num.setText("1");
                            } else {
                                currentItem++;

                                viewP.setCurrentItem(currentItem);
                            }
                        }
                    });
                }
            }, 4000, 4000);
            viewP.setOnTouchListener(new View.OnTouchListener() {
                @Override
                public boolean onTouch(View v, MotionEvent event) {
                    int action = event.getAction();

                    switch (action) {
                        case MotionEvent.ACTION_DOWN:
                            //按下
                            //停止定时器
                            timer1.cancel(); //关闭
                            break;

                        case MotionEvent.ACTION_UP:
                            //抬起
                            //开启定时器
                            timer1 = new Timer(); //新创建一个对象
                            timer1.schedule(new TimerTask() {

                                @Override
                                public void run() {
                                    // TODO Auto-generated method stub

                                    getActivity().runOnUiThread(new Runnable() {
                                        // 主线程
                                        @Override
                                        public void run() {
                                            // TODO Auto-generated method stub
                                            int currentItem = viewP.getCurrentItem(); // 得到ViewPager当前显示第几页

                                            int count = adapter.getCount(); // 总共有多少条

                                            // 0123 4
                                            if (currentItem == (count - 1)) {
                                                // 如果使用第二个参数，单条显示Fragment数据可能无法加载！
                                                viewP.setCurrentItem(0);
                                                num.setText("1");
                                            } else {
                                                currentItem++;

                                                viewP.setCurrentItem(currentItem);
                                            }
                                        }
                                    });


                                }
                            }, 4000, 4000);

                            break;
                    }
                    return false;
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void getData(String path2) {
        x.http().get(new RequestParams(path2), new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                Gson gson = new Gson();
                DTBean dtBean = gson.fromJson(result, DTBean.class);
                data2 = dtBean.getSkuList();
                String skuNames = data2.get(0).getSkuName();
                skuName.setText(skuNames);
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }


    private void getImgData(String path) {
        RequestParams params = new RequestParams(path);
        x.http().get(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                Gson gson = new Gson();
                GDTPBean bean = gson.fromJson(result, GDTPBean.class);
                skuImgList = bean.getSkuImgList();
                int n = skuImgList.size();

                int size = skuImgList.get(0).getImgBigList().size();

                data = skuImgList.get(0).getImgBigList();
                Log.e("sssss", data.get(0));
                viewP.setOffscreenPageLimit(size-1);

                adapter = new GDTPAdapter(getActivity(), data);
                viewP.setAdapter(adapter);
                viewP.setCurrentItem(0);
                String sss = Integer.toString(size);
                numAll.setText("/" + sss);
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }
    @Override
    public void onDestroy() {
        super.onDestroy();
        timer1.cancel();
    }

}
